﻿using System;
using System.Configuration;
using System.Windows.Controls;

namespace ViewLED.UI
{
    /// <summary>
    /// Psetting.xaml 的交互逻辑
    /// </summary>
    public partial class Psetting : Page
    {
        public Psetting()
        {
            // 窗体组件初始化
            InitializeComponent();
            // 设置选项初始化
            initializeSettings();
        }

        // 从app.config中读取当前配置并显示在界面上
        private void initializeSettings()
        {
            imgType.SelectedIndex = int.Parse(ConfigurationManager.AppSettings["ImageStretchMode"]);
            videoType.SelectedIndex = int.Parse(ConfigurationManager.AppSettings["VideoStretchMode"]);
            sLeft.Text = ConfigurationManager.AppSettings["ScreenPosLeft"];
            sTop.Text = ConfigurationManager.AppSettings["ScreenPosTop"];
            sWidth.Text = ConfigurationManager.AppSettings["ScreenWidth"];
            sHeight.Text = ConfigurationManager.AppSettings["ScreenHeight"];
            cbxSpeed.SelectedIndex = int.Parse(ConfigurationManager.AppSettings["SwitchTime"]);
        }

        // 校验设置项
        private bool validateSettings()
        {
            double v;  // 记录将文本将化为double后的值
            bool f;    // 标记是否是合法的格式值
            f = double.TryParse(sLeft.Text, out v);
            if (v < 0 || v > System.Windows.SystemParameters.PrimaryScreenWidth) 
            {
                showError("屏幕初始位置（X）");
                return false;
            }
            f = double.TryParse(sTop.Text, out v);
            if (v < 0 || v > System.Windows.SystemParameters.PrimaryScreenHeight)
            {
                showError("屏幕初始位置（Y）");
                return false;
            }
            f = double.TryParse(sWidth.Text, out v);
            if (v < 0 || v + double.Parse(sLeft.Text) > System.Windows.SystemParameters.PrimaryScreenWidth)
            {
                showError("屏幕宽度（X）");
                return false;
            }
            f = double.TryParse(sHeight.Text, out v);
            if (v < 0 || v + double.Parse(sTop.Text) > System.Windows.SystemParameters.PrimaryScreenHeight)
            {
                showError("屏幕高度（Y）");
                return false;
            }
            return true;
        }

        // 保存
        private void save(object sender, EventArgs e)
        {
            if (validateSettings())
            {
                Model.FileExplorer.setConfig("ImageStretchMode", imgType.SelectedIndex.ToString());
                Model.FileExplorer.setConfig("VideoStretchMode", videoType.SelectedIndex.ToString());
                Model.FileExplorer.setConfig("SwitchTime", cbxSpeed.SelectedIndex.ToString());
                Model.FileExplorer.setConfig("ScreenPosLeft", sLeft.Text);
                Model.FileExplorer.setConfig("ScreenPosTop", sTop.Text);
                Model.FileExplorer.setConfig("ScreenWidth", sWidth.Text);
                Model.FileExplorer.setConfig("ScreenHeight", sHeight.Text);
                System.Windows.Forms.MessageBox.Show("已保存成功！", "提示信息");
            }
        }

        // 放弃
        private void cancel(object sender, EventArgs e)
        {
            initializeSettings();
            System.Windows.Forms.MessageBox.Show("本次修改已被恢复到原设置！", "提示信息");
        }

        // 错误提示
        private void showError(string item)
        {
            System.Windows.Forms.MessageBox.Show(item + "的值不符合要求，请修改！", "提示信息");
        }
    }
}
